package medium;

import java.util.*;

/**
 * 46. 全排列
 * 创建时间：2024-05-21 15:32
 */
public class 全排列_46 {
    static class Solution {
        List<List<Integer>> ans = new ArrayList<>();
        boolean[] onPath; // 用来标注当前位置是否已获取
        public List<List<Integer>> permute(int[] nums) {
            onPath = new boolean[nums.length];
            dfs(0, nums, Arrays.asList(new Integer[nums.length]));
            return ans;
        }

        void dfs(int index, int[] nums, List<Integer> path) {
            if (index == nums.length) {
                ans.add(new ArrayList<>(path));
                return;
            }
            for (int i = 0; i < nums.length; i++) {
                if (!onPath[i]) {
                    onPath[i] = true;
                    path.set(index, nums[i]);
                    dfs(index + 1, nums, path);
                    onPath[i] = false;
                }
            }
        }
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.permute(new int[]{1,2,3}));
    }
}
